package sysAuth

import sysUtil.ResultCode
import sysUtil.ResultJson
import sysUtil.SysCommUtil
import xdInterface.XdControllerInterface

class CollectionController implements XdControllerInterface {

    def index() {
        query()
    }

    @Override
    def detail() {
        return null
    }

    @Override
    def del() {
        def result = ResultJson.resultJson()
        try {
            def id = params.id
            def  syscol = SysCollection.get(Integer.parseInt(id))
            if(syscol){
                syscol.delete(flush: true)
            }

            result.put(ResultJson.CODE.name, ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_SUCCESS.message)
        } catch (Exception e) {
            result.put(ResultJson.CODE.name, ResultCode.OPT_ERROR.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_ERROR.message)
            e.printStackTrace()
        } finally {
            //println result;
            render result.toString()
        }
    }

    @Override
    def edit() {
        return null
    }

    @Override
    def save() {
        return null
    }

    @Override
    def update() {
        return null
    }

    @Override
    def add() {
        def result = ResultJson.resultJson()
        try {

            def userNo = params.userNo
            //path:path,type:"SysFunctions"
            def type = params.type?:"sysAuth.SysFunctions"
            if( type == "sysAuth.SysFunctions"){
                def path = String.valueOf(params.path?:"")
                def sysFunc = SysFunctions.findAllByPathIsNotNull();
                for(def func : sysFunc){
                    if(path.indexOf(func.path)!=-1){
                        def sysCon =  SysCollection.findByDataIdAndTypeAndCreator(func.id,type,userNo)
                        if(sysCon){
                            sysCon.setDataId(func.id)
                            sysCon.save(flush: true)
                        }else{
                            new SysCollection(dataId: func.id,type: type,creator: userNo,updater: userNo).save(flush: true)
                        }
                        break
                    }
                }
            }
            result.put(ResultJson.CODE.name, ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_SUCCESS.message)
        } catch (Exception e) {
            result.put(ResultJson.CODE.name, ResultCode.OPT_ERROR.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_ERROR.message)
            e.printStackTrace()
        } finally {
            //println result;
            render result.toString()
        }
    }

    @Override
    def dowLoad() {

    }

    @Override
    def upLoad() {
        return null
    }

    @Override
    def query() {
        def result = ResultJson.resultJson()
        try {
            def userNo = params?.userNo
            //path:path,type:"SysFunctions"
            def type = params.type?:"sysAuth.SysFunctions"
            //StringBuffer sb  =  new StringBuffer()
            def sb = []
            if( type.indexOf("SysFunctions")!=-1 ){

                def clist  =  SysCollection.findAllByCreator(userNo,[order:"desc",sort:"lastUpdated"])
                if(clist){
                    //sb =   SysFunctions.findAllByIdInList(clist.dataId)
                    clist.each {
                        SysFunctions functions =  SysFunctions.get(Long.parseLong(it.dataId))
                        if(functions){
                            sb.add([rptId:functions.rptId,funcName:functions.funcName,path:functions.path,funcId:functions.id,
                                    icon:functions.icon,collectId:it.id])
                        }
                    }
                }
            }
            result.put(ResultJson.CODE.name, ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_SUCCESS.message)
            result.put(ResultJson.DATA.name,SysCommUtil.copyMapList(sb))
        } catch (Exception e) {
            result.put(ResultJson.CODE.name, ResultCode.OPT_ERROR.code)
            result.put(ResultJson.MSG.name, ResultCode.OPT_ERROR.message)
            e.printStackTrace()
        } finally {
            //println result;
            render  result.toString()
        }
    }

    @Override
    def delete() {
       del()
    }

    @Override
    def getOne() {
        return null
    }
}
